<template>
    <div class="manage">
        <el-dialog
            :title="operateType === 'add' ? '新增用户' : '更新用户'"
            :visible.sync="isShow"
        >
            <common-form
                :formLabel="operateFormLabel"
                :form="operateForm"
                :inline="true"
                ref="form"
            ></common-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="isShow = false">取消</el-button>
                <el-button type="primary" @click="confirm">确定</el-button>
            </div>
        </el-dialog>
        <div class="manage-header">
            <el-button type="primary" @click="addUser">+ 新增</el-button>
            <common-form
                :formLabel="formLabel"
                :form="searchForm"
                :inline="true"
                ref="form"
            >
                <el-button type="primary" @click="getList(searchForm.keyWord)">搜索</el-button>
            </common-form>
        </div>
        <common-table
            :tableData="tableData"
            :tableLabel="tableLabel"
            :config="config"
            @changePage="getList()"
            @edit="editUser"
            @del="delUser"
        ></common-table>
    </div>
</template>

<script>
import CommonForm from '../../components/common-form/index.vue'
import CommonTable from '../../components/common-table/index.vue'
import  { getUser } from '../../api/data'
export default {
    name: 'User',
    components: {
        CommonForm,
        CommonTable
    },
    data() {
        return {
            operateType: 'add',
            isShow: false,
            operateFormLabel:[
                {
                    model: 'name',
                    label: '姓名',
                    type: 'input'
                },
                {
                    model: 'age',
                    label: '年龄',
                    type: 'input'
                },
                {
                    model: 'sex',
                    label: '性别',
                    type: 'select',
                    opts: [
                        {
                            label: '男',
                            value: 1
                        },
                        {
                            label: '女',
                            value: 0
                        }
                    ]
                },
                {
                    model: 'birth',
                    label: '出生日期',
                    type: 'date'
                },
                {
                    model: 'addr',
                    label: '地址',
                    type: 'input'
                }
            ],
            operateForm: {
                name: '',
                addr: '',
                age: '',
                birth: '',
                sex: '',
            },
            //表单的配置
            formLabel:[
                {
                    model: 'keyWord',
                    label: '',
                    type: 'input'
                }
            ],
            searchForm: {
                keyWord: ''
            },
            //table表格的数据
            tableData: [],
            //table列的配置数据
            tableLabel: [
                {
                    prop: "name",
                    label: "姓名"
                },
                {
                    prop: "age",
                    label: "年龄"
                },
                {
                    prop: "sexLabel",
                    label: "性别"
                },
                {
                    prop: "birth",
                    label: "出生日期",
                    width: 200
                },
                {
                    prop: "addr",
                    label: "地址",
                    width: 320
                },
            ],
            //分页的配置
            config: {
                page: 1,
                total: 30
            }
        }
    },
    methods: {
        //提交表单
        confirm(){
            if(this.operateType === 'edit') {
                //调用接口编辑
                this.$axios.post('/user/edit', this.operateForm).then(res => {
                    console.log(res);
                    this.isShow = false
                    this.getList()
                })
            } else {
                this.$axios.post('/user/add', this.operateForm).then(res => {
                    console.log(res);
                    this.isShow = false
                    this.getList()
                })
            }
        },
        //增加用户
        addUser(){
            this.isShow = true
            this.operateType = 'add'
            this.operateForm = {
                name: '',
                addr: '',
                age: '',
                birth: '',
                sex: '',
            }
        },  
        //编辑用户信息
        editUser(row){
            this.operateType = 'edit'
            this.isShow = true
            this.operateForm = row
        },
        //删除用户信息
        delUser(row){
            //通知栏组件
            this.$confirm("此操作将永久删除该组件，是否继续", "提示",{
                confirmButtonText: "确认", //确认按钮文本
                cancleButtonText: "取消",   //取消按钮文本
                type: "warning" //弹窗类型
            }).then(() => {
                //当点击是所触发的回调
                const id = row.id
                this.$axios.post("/user/del", {
                    params: { id }
                }).then(() => {
                    //回调后返回的数据
                    //弹出的文本
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                    this.getList()
                })
            })
        },
        getList(name = ''){
            this.config.loading = true
            name ? (this.config.page = 1) : ''
            getUser({
                page: this.config.page,
                name
            }).then(({data: res}) => {
                //处理table数据'
                console.log(res, 'res')
                this.tableData = res.list.map(item => {
                    item.sexLabel = item.sex === 0 ? "女" : "男"
                    return item
                })
                //处理分页数据
                this.config.total = res.count
                this.config.loading = false
            })
        }
    },  
    //一创建时调用
    created(){
        this.getList()
    }
};
</script>

<style lang="less" scoped>
    .manage-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
</style>